﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace QunYing.DAL
{
    /// <summary>
    /// 数据访问类:ActivityGoods
    /// </summary>
    public partial class ActivityGoods
    {
        /// <summary>
        /// 获取活动内容
        /// </summary>
        /// <param name="top"></param>
        /// <returns></returns>
        public DataTable GetActivityGoods(int top, string DealerID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT TOP ("+top+@") 活动ID ,
	        a.商品ID ,
	        宣传图片 ,
	        活动内容 ,
	        开始时间 ,
	        结束时间,
	        类型=(CASE WHEN b.是否推荐=1 THEN 'hot' ELSE CASE WHEN b.是否折价=1 THEN 'sale' ELSE 'score' END END)
            from dbo.ActivityGoods a
            LEFT JOIN dbo.GoodsInfo b
            ON a.商品ID=b.商品ID
            WHERE ((GETDATE() BETWEEN a.开始时间 AND a.结束时间) OR (a.开始时间<=GETDATE() AND a.结束时间 IS NULL)) AND a.经销商编号='" + DealerID + @"'
             ORDER BY a.显示顺序 ASC");
            return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        }

        /// <summary>
        /// 获取活动内容
        /// </summary>
        /// <param name="top"></param>
        /// <returns></returns>
        public DataTable GetActivityGoodsByCache(int top, string DealerID)
        {
            string CacheKey = "GetActivityGoods-" + top + DealerID;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = GetActivityGoods(top,DealerID);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (DataTable)objModel;
        }
    }
}

